const express = require('express');
const router = express.Router();
const db = require('../../../../util/dbConfig/dbConfig')


//修改监测信息
router.post('/',async (req,res) => {
    if(req.session.userInfo.admin !==1 || !req.session.userInfo){
        return res.send({
            code: 3,
            msg: '非管理员权限或未登录'
        })
    }

    let {
        sheepId,  //羊只编号
        level,     //等级
        year,  //年度
        bodyHight,   //体高
        bodyLength,    //体长
        bust,        //胸围
        bodyWeight,  //体重
        pileLength,     //绒长度
        pileWide,       //绒细度
        pileYield,       //绒产量
        tube,            //管围
        pileThickness,      //绒厚
        remake              //备注
    }=req.body

    let arr = [
        sheepId,  //羊只编号
        level,     //等级
        year,  //年度
        bodyHight,   //体高
        bodyLength,    //体长
        bust,        //胸围
        bodyWeight,  //体重
        pileLength,     //绒长度
        pileWide,       //绒细度
        pileYield,       //绒产量
        tube,            //管围
        pileThickness,      //绒厚
        remake              //备注
    ]  //将req.body中的数据封装成数组

    if(arr.length === 0){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    try{
        let updateSql = `update production set YEAR=${year},BODYTALL=${bodyHight},BODYLONG=${bodyLength},CHEST=${bust},WEIGHT=${bodyWeight},LENGTH=${pileLength},FINE=${pileWide},RONGYIELD=${pileYield},TANK='${level}',PIPE=${tube},TINCK_WOOL=${pileThickness},REMARK=${remake} where SHEEP_ID=${sheepId}`
        let updateSqlArr = []
        let result = await db.SySqlConnect(updateSql,updateSqlArr)
        if(result.changedRows !==0){
            return res.send({
                code: 0,
                msg: '修改成功'
            })
        }else {
            return res.send({
                code: 2,
                msg: '修改失败'
            })
        }
    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误,请稍后再试'
        })
    }
})

module.exports = router
